<template>
  <el-dialog
    :title="dataForm.id ? '修改' : '新增'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="60%"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="110px"
    >
      <el-form-item label="账号" prop="username">
        <el-input v-model="dataForm.username" placeholder="账号"></el-input>
        <div>默认密码：123456</div>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="手机"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="dataForm.role" style="width: 100%">
          <el-option v-for="item in roleOptions" :key="item.id" :label="item.role" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker
          v-model="dataForm.birthday"
          type="date"
          value-format="yyyy-MM-dd"
          clearable
          :disable="dataForm.role==2"
          placeholder="选择日期"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="毕业院校" prop="graduation_inst">
        <el-input v-model="dataForm.graduation_inst" :disable="dataForm.role==2" placeholder="毕业院校"></el-input>
      </el-form-item>
      <el-form-item label="学历" prop="education_qual">
        <el-input v-model="dataForm.education_qual" :disable="dataForm.role==2" placeholder="学历"></el-input>
      </el-form-item>
      <el-form-item label="职称" prop="professional_title">
        <el-input
          v-model="dataForm.professional_title"
          :disable="dataForm.role==2"
          placeholder="职称"
        ></el-input>
      </el-form-item>
      <el-form-item label="职业资格证书" prop="professional_cert">
        <el-upload
          class="upload-demo"
          action
          accept="image/*"
          :http-request="handleUpload"
          :before-upload="beforeUpload"
          list-type="picture-card"
          :on-remove="handleRemove"
          :fileList="fileList"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getDataInfo, savaDataInfo } from "@/api/user";
import { getRoleOptions } from "@/api/role";
import { upload } from "@/api/pet";
export default {
  data() {
    return {
      visible: false,
      dataForm: {},
      dataRule: {
        username: [
          { required: true, message: "账号不能为空", trigger: "blur" }
        ],
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        role: [{ required: true, message: "角色不能为空", trigger: "blur" }],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        phone: [{ required: true, message: "手机不能为空", trigger: "blur" }],
        address: [{ required: true, message: "住址不能为空", trigger: "blur" }]
      },
      roleOptions: [],
      fileList: []
    };
  },
  mounted() {
    getRoleOptions()
      .then(res => {
        if (res && res.code === 0) {
          this.roleOptions = res.data;
        }
      })
      .catch(() => {});
  },
  methods: {
    beforeUpload(file) {
      if (!file.type.startsWith("image")) {
        this.$message.warning("请上传图片");
        return false;
      }
    },
    // 上传图片
    handleUpload(file) {
      let formData = new FormData();
      formData.append("file", file.file);
      upload(formData).then(({ fileInfo }) => {
        fileInfo.filePath ? this.fileList.push({ url: fileInfo.filePath }) : "";
        this.dataForm.professional_cert = fileInfo.filePath || "";
      });
    },
    // 删除图片
    handleRemove(file) {
      this.fileList = this.fileList.filter(function(item) {
        return item.url !== file.url;
      });
    },
    init(username) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.dataForm = { username: username };
        if (this.dataForm.username) {
          getDataInfo(this.dataForm.username)
            .then(data => {
              if (data && data.code === 0) {
                this.dataForm = data.data;
                this.$set(this.dataForm, "role", data.data.roleList[0].roleId);
                this.fileList = data.data.professional_cert
                  ? [
                      {
                        url: data.data.professional_cert || ""
                      }
                    ]
                  : [];
              }
            })
            .catch(err => {
              console.log(err);
            });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.dataForm.id
            ? delete this.dataForm.password
            : (this.dataForm.password = "123456");
          savaDataInfo(this.dataForm)
            .then(data => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  }
                });
              }
            })
            .catch(err => {
              console.log(err);
            });
        }
      });
    }
  }
};
</script>